# coding=utf-8
import os
import random
import time

def wash_time(func):
    # 模块名，此例中为__main__
    # print __name__
    # 打印当前文件路径
    # print __file__

    def wrapper(*args, **kwargs):
        start_time = time.time()
        func(*args, **kwargs)
        end_time = time.time()

        # 打印上层调用函数名
        print func.__name__, "spend time", "{:.2f}".format(end_time - start_time), "s"

    return wrapper


@wash_time
def upload(file_name="my_file"):
    print "upload", file_name, "start"
    time.sleep(random.randint(2, 4))
    print "success"


@wash_time
def download(file_name="my_file"):
    print "download", file_name, "start"
    time.sleep(random.randint(2, 4))
    print "success"


if __name__ == '__main__':
    '''
        装饰函数
        分别计算up和down一个文件的时间
        使用if()分支的函数，up和down计算时间差的代码重复，
        else()分支使用装饰函数，将计算时间差的同一
    '''
    if 0:
        start = time.time()
        upload("file_up")
        end = time.time()
        print "upload success with", end - start, "second"
        start = time.time()
        upload("file_down")
        end = time.time()
        print "download success with", end - start, "second"
    else:
        # download()->wash_time->download()
        download("filedw.pdf")
        # upload()->wash_time->upload()
        upload("fileup.txt")
    os.system("pause")
